﻿Imports System.Data.OleDb
Public Class Forum
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringRestaurants").ConnectionString)
            'select all fields from Forum table
            Dim SqlString As String = "SELECT * FROM ForumView"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            oleDbConn.Open()
            Dim dataReader = cmd.ExecuteReader()
            GridView1.DataSource = dataReader
            GridView1.DataBind()
        End If
    End Sub



    Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Submit.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionStringRestaurants").ConnectionString)
        Dim SqlString As String = "Insert into Forum(Discussion_title,Comment,UserName) Values (@f1,@f2,@f3)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_DiscussionTitle.Text)
        cmd.Parameters.AddWithValue("@f2", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'fill GridView1 with comments
        Dim fillComments As String = "SELECT * FROM ForumView"
        Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommentsCmd.CommandType = CommandType.Text
        Dim dataReader = fillCommentsCmd.ExecuteReader()
        GridView1.DataSource = dataReader
        GridView1.DataBind()
        'this will show all comments
        'clear comment fields
        tb_Comment.Text = ""
        tb_DiscussionTitle.Text = ""

     


    End Sub
End Class